const common_url = 'http://192.168.1.104:8000/';  //服务器地址
const token = '';   //用户登陆后返回的token
/**
 * 使用fetch实现图片上传
 * @param {string} url  接口地址
 * @param {JSON} params body的请求参数
 * @return 返回Promise
 */
export const uploadImage = (url:string,params:any) => {
    return new Promise(function (resolve, reject) {
        let formData = new FormData();
        for (var key in params) {
            formData.append(key, params[key]);
        }
        let file = {uri: params.path, type: "application/octet-stream", name: 'image.jpg'};
        formData.append("file", file);
        fetch(common_url + url, {
            method: 'POST',
            headers: {
                'Content-Type': 'multipart/form-data;charset=utf-8',
                // 'Content-Type': 'application/json',
                // 'Content-Type': 'multipart/form-data',
                "x-access-token": token,
            },
            body: formData,
            // body: JSON.stringify(params),
        }).then((response) => response.json())
            .then((responseData) => {
                console.log('uploadImage', responseData);
                resolve(responseData);
            })
            .catch((err) => {
                console.log('err', err);
                reject(err);
            });
    });
}
